package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.RecomHotInfo;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/5/19.
 */
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> {

    private Context mContext;
    private List<RecomHotInfo> infos;
    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, RecomHotInfo info);
    }

    public RecommendAdapter(Context context) {
        this.mContext = context;
        infos = new ArrayList<>();
    }

    public void addAll(List<RecomHotInfo> infos) {
        this.infos.addAll(infos);
        notifyDataSetChanged();
    }

    public void clear() {
        this.infos.clear();
        notifyDataSetChanged();
    }

    public void add(RecomHotInfo info) {
        this.infos.add(info);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_meeting_t, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) * 103 / 400, ScreenUtils.getScreenHeight(parent.getContext()) * 5 / 12);
        view.setLayoutParams(la);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, infos.get(position));
                }
            }
        });

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(infos.get(position).getSTART_TIME()) && infos.get(position).getSTART_TIME().length() > 10) {
            try {
                date_start = sdf.format(Date.valueOf(infos.get(position).getSTART_TIME().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start);
                holder.mTime.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.mTime.setText(infos.get(position).getSTART_TIME());
            }
        } else {
            holder.mTime.setText("待确定");
        }


        ImageUtils.loadImage(mContext, infos.get(position).getCLIENT_BANNER(), holder.mImageView);
        holder.mTitle.setText(infos.get(position).getNAME());
//        holder.mTime.setText(infos.get(position).getCREATE_AT());
        if (TextUtils.isEmpty(infos.get(position).getPROVICE_NAME()) && TextUtils.isEmpty(infos.get(position).getCITY_NAME())) {
            holder.mPosition.setText("其他地区");
        }
        if (TextUtils.isEmpty(infos.get(position).getCITY_NAME())) {
            infos.get(position).setCITY_NAME("");
        }
        holder.mPosition.setText(infos.get(position).getPROVICE_NAME() + infos.get(position).getCITY_NAME());
        if (infos.get(position).getCONSTATUS().equals("报名截止")) {
            holder.mStatus.setVisibility(View.INVISIBLE);
            holder.mStatus.setText(infos.get(position).getCONSTATUS());
        } else {
            holder.mStatus.setVisibility(View.VISIBLE);
            holder.mStatus.setText(infos.get(position).getCONSTATUS());
        }


    }

    @Override
    public int getItemCount() {
        if (infos.size() > 4) {
            return 4;
        }
        return infos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;
        private TextView mTitle;
        private TextView mTime;
        private TextView mPosition;
        private TextView mStatus;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.imv_meeting);
            mTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            mTime = (TextView) itemView.findViewById(R.id.txt_item_date);
            mPosition = (TextView) itemView.findViewById(R.id.txt_item_address);
            mStatus = (TextView) itemView.findViewById(R.id.txt_item_meeting_state);
        }
    }
}
